/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package modelController.sessionController;

import entities.School;
import entities.Student;
import entities.Studenttestpaper;
import entities.Subject;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author Administrator
 */
@Named("courseCollabrativeFilter")
@SessionScoped
public class CourseCollabrativeFilter implements java.io.Serializable {

    @Inject
    modelController.applicationController.SubjectController applicationSubjectController;
    @Inject
    modelController.applicationController.SchoolController applicationSchoolController;

    private HashMap<Student, HashMap<Subject, Double>> studentScoreMap;

    private HashMap<Subject, Double> getStudentScore(Student student) {
        if (null == studentScoreMap) {
            studentScoreMap = new HashMap<>();
        }
        if (null == studentScoreMap.get(student)) {
            HashMap<Subject, List<Double>> subjectScoreHashMap = new HashMap<>();
            Set<Studenttestpaper> studenttestpaper = student.getStudenttestpaperSet();
            HashMap<Subject, Double> studentSubScore = new HashMap<>();
            for (Studenttestpaper stp : studenttestpaper) {
                String subjectids = stp.getTestpaperId().getSubjectids();
                String sbujectString[] = subjectids.split(",");
                for (int i = 0; i < sbujectString.length; i++) {
                    Subject subject = applicationSubjectController.find(Integer.valueOf(sbujectString[i]));
                    if (subjectScoreHashMap.get(subject) == null) {
                        List<Double> score = new LinkedList<>();
                        subjectScoreHashMap.put(subject, score);
                    }
                    subjectScoreHashMap.get(subject).add(stp.getTestscore());
                }
            }
            //把线性表中的多次成绩求平均
            for (Entry entry : studentSubScore.entrySet()) {
                int count = ((List<Double>) entry.getValue()).size();
                List<Double> score = (List<Double>) entry.getValue();
                double tem = 0;
                for (int i = 0; i < count; i++) {
                    tem += score.get(i);
                }
                tem = tem / count;
                studentSubScore.put((Subject) entry.getKey(), tem);
            }
        }
        return studentScoreMap.get(student);
    }
    
    public  void outputMatrix(){
        //把所有学生的对应课程的成绩都打印出来
        List<School> allSchools=applicationSchoolController.getAllList();
        for(School school:allSchools){
            for(Student student: school.getStudentSet()){
                HashMap<Subject,Double> studentScoreHashMap=getStudentScore(student);
                for (Entry entry: studentScoreHashMap.entrySet()) {
                    System.out.print(((Subject)entry.getKey()).getName()+":"+((Double)entry.getValue()));
                }
            }
        }
    }
}
